<?php

namespace app\marketing\controller;

use \app\marketing\model\Poster as PosterModel;
use app\AdminRest;
use app\marketing\model\PosterType;

class AdminPoster extends AdminRest
{
    public function listPoster()
    {
        //筛选部门信息
        $param = $this->_param;
        //获取分页信息
        $page_config = array(
            'page' => 1,
            'page_count' => 20
        );
        //设置页码
        if (isset($param['page']) && $param['page'] > 0) $page_config['page'] = $param['page'];
        //设置每页的数据
        if (isset($param['page_count']) && $param['page_count'] > 0) $page_config['page_count'] = $param['page_count'];
        $filter[] = ['uniacid', '=', $this->_uniacid];

        $filter[] = ['status', '<>', -1];

        do {
            if (isset($param['type_id']) && $param['type_id'] > 0) {
                $filter[] = ['type_id', '=', $param['type_id']];
                break;
            }


            if ($param['type_id'] == -1) {
                $m_poster_type = new PosterType();

                $type_ids_deleted = $m_poster_type->where([['status', '<>', 1], ['uniacid', '=', $this->_uniacid]])->field('id')->select()->toArray() ?? [];
                $type_ids_deleted = array_column($type_ids_deleted, 'id');
                $filter[] = ['type_id', 'IN', $type_ids_deleted];
                break;
            }
        } while (false);


        $m_poster = new PosterModel();
        $page_config['total'] = $m_poster->listPostersCount($filter);
        $posters = $m_poster->listPosters($filter, $page_config);

        $type_ids = array_column($posters, 'type_id');
        $m_type = new PosterType();
        $poster_type_temp = $m_type->where([['id', 'in', $type_ids], ['status', '=', 1], ['uniacid', '=', $this->_uniacid]])->select();
        $poster_type = [];
        foreach ($poster_type_temp as $v) {
            $poster_type[$v['id']] = $v['title'];
        }

        foreach ($posters as $k => $v) {
            $posters[$k]['type_title'] = $poster_type[$v['type_id']] ?? '暂无分类';
            $posters[$k]['cover'] = $v['cover'] ?? $v['img'] ?? '';
        }

        $posters = transImagesOne($posters, ['img', 'cover']);
        $page_config['total_page'] = (int)($page_config['total'] / $page_config['page_count']);
        if (($page_config['total'] % $page_config['page_count']) > 0) $page_config['total_page'] = $page_config['total_page'] + 1;
        //设置返回参数
        $result = $page_config;
        //返回数据
        $result['data'] = $posters;
        return $this->success($result);
    }

    public function createPoster()
    {
        $input = $this->_input;
        $title = $input['title'] ?? null;
        if (empty($title))
        {
            return $this->error('title is required');
        }

        $type_id = $input['type_id'] ?? null;
        if (!$type_id)
        {
            return $this->error('type_id is required');
        }
        $input['uniacid'] = $this->_uniacid;
        $poster = new PosterModel();
        $res = $poster->createPoster($input);
        return $this->success($res);
    }

    public function updatePoster()
    {
        $poster_id = $this->_param['id'] ?? null;
        if (!$poster_id)
        {
            return $this->error('poster_id is required');
        }
        $input = $this->_input;
        $poster = new PosterModel();
        $data = $poster->updatePoster(['id' => $poster_id], $input);
        return $this->success($data);
    }

    public function getPoster()
    {
        $poster_id = $this->_param['id'] ?? null;
        if (!$poster_id)
        {
            return $this->error('poster_id is required');
        }
        $poster = new PosterModel();
        $data = $poster->getPoster(['id' => $poster_id]);
        $type_id = $data['type_id'] ?? null;
        $m_post_type = new PosterType();
        if (!$m_post_type->where([['id', '=', $type_id], ['status', '=', 1], ['uniacid', '=', $this->_uniacid]])->count()) {
            $data['type_id'] = "";
        }
        $data = transImagesOne($data, ['img', 'cover']);
        return $this->success($data);
    }

    public function changeStatus()
    {
        $poster_id = $this->_param['id'] ?? null;
        if (!$poster_id)
        {
            return $this->error('poster_id is required');
        }
        $input = $this->_input;
        $model = new PosterModel();
        $rst = $model->updatePoster(['id' => $poster_id], ['status' => $input['status']]);
        return $this->success($rst);
    }
}
